package stdio.costa.imoca.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import stdio.costa.imoca.model.MocaAccessToken;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController {

	@RequestMapping(value = "/signup")
	public String signup(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {
		try {
			String name = request.getParameter("name");
			String password = request.getParameter("password");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String realname = request.getParameter("realname");

			ClientConfig config = new DefaultClientConfig();
			Client client = Client.create(config);
			WebResource service = client.resource(getBaseURI());
			MultivaluedMap<String, String> param = new MultivaluedMapImpl();

			param.add("name", name);
			param.add("password", password);
			param.add("email", email);
			param.add("phone", phone);
			param.add("realname", realname);

			MocaAccessToken mocaAccessToken = service.path("mocauser")
					.path("signup").queryParams(param)
					.type(MediaType.APPLICATION_FORM_URLENCODED)
					.accept(MediaType.APPLICATION_XML)
					.post(MocaAccessToken.class);

			request.getSession().setAttribute("accessTokenCode",
					mocaAccessToken.getAccessToken());

			result.put("uid", mocaAccessToken.getUid());
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return "error";
		}

		return "login_success";
	}

	@RequestMapping(value = "/login")
	public String appLogin(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String username = request.getParameter("loginname");
		String password = request.getParameter("password");
		String logintype = request.getParameter("signup_type");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("login_key", username);
		param.add("password", password);
		param.add("login_type", logintype);

		MocaAccessToken accessToken = service.path("mocauser").path("login")
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(MocaAccessToken.class);

		if (accessToken != null) {
			request.getSession(false).setAttribute("accessToken",
					accessToken.getAccessToken());
			result.put("uid", accessToken.getUid());
			return "login_success";
		}

		return null;
	}
	
	@RequestMapping(value = "/forget_password")
	public String forgetPassword(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {
		String loginKey = request.getParameter("login_key");
		
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("login_key", loginKey);
		
		service.path("mocauser").path("forget_password")
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).post();
		
		return null;
	}
}
